package com.youdoneed.user.role;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.user.model.Authority;
import com.youdoneed.user.model.Role;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 角色请求控制器。
 *
 * @author Ewing
 * @date 2017.2.8
 */
@Controller
@RequestMapping("/role")
@Api(description = "角色")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色信息，添加权限与角色的关联关系。
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage addRole(String name, String description, @RequestParam(value = "authorityIds[]") String[] authorityIds) {
        roleService.addRole(name, description, authorityIds);
        return new ResultMessage();
    }

    /**
     * 编辑角色信息，添加权限与角色的关联关系。
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage updateRole(String roleId, String name, String description, @RequestParam(value = "authorityIds[]") String[] authorityIds) {
        roleService.updateRole(roleId, name, description, authorityIds);
        return new ResultMessage();
    }

    /**
     * 删除角色，先删除角色与权限的关联关系以及角色与管理员的关联关系。
     *
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage deleteRole(String roleId) {
        roleService.deleteRole(roleId);
        return new ResultMessage();
    }

    /**
     * 查询所有角色。
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<List<Role>> getAllRole() {
        List<Role> roles = roleService.getAllRole();
        return new ResultMessage<>(roles);
    }

    /**
     * 根据角色名称查找角色
     */
    @ResponseBody
    @RequestMapping(value = "/getRoleByName", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage getRoleByName(String name) {
        List<Role> listRole = roleService.findByName(name);
        return new ResultMessage<>(listRole);
    }

    /**
     * 根据ID查询单条角色。
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Role> getRole(String roleId) {
        Role role = roleService.findByRoleId(roleId);
        if (null == role) {
            return new ResultMessage<Role>().toFailure("没有查询到对应记录！");
        }
        return new ResultMessage<>(role);
    }

    /**
     * 根据角色ID查找所有的权限
     */
    @ResponseBody
    @RequestMapping(value = "/getAuthorityByRoleId", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<List<Authority>> getAuthorityByRoleId(String roleId) {
        List<Authority> authorities = roleService.getRoleAllAuthority(roleId);
        return new ResultMessage<>(authorities);
    }

}
